-
Notifications
You must be signed in to change notification settings - Fork 15.6k
[clang-format] Allow Language: Cpp for C files
#133033
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Member
|
@llvm/pr-subscribers-clang-format Author: Owen Pan (owenca) ChangesFix #132832 Full diff: https://github.com/llvm/llvm-project/pull/133033.diff 2 Files Affected:
diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index c67db4752e87b..87e36578d9958 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -2129,10 +2129,17 @@ std::error_code parseConfiguration(llvm::MemoryBufferRef Config,
LanguageFound = true;
}
if (!LanguageFound) {
- if (Styles.empty() || Styles[0].Language != FormatStyle::LK_None)
+ if (Styles.empty())
return make_error_code(ParseError::Unsuitable);
- FormatStyle DefaultStyle = Styles[0];
- DefaultStyle.Language = Language;
+ auto DefaultStyle = Styles[0];
+ auto &DefaultLanguage = DefaultStyle.Language;
+ if (DefaultLanguage != FormatStyle::LK_None &&
+ // For backward compatibility.
+ !(DefaultLanguage == FormatStyle::LK_Cpp &&
+ Language == FormatStyle::LK_C)) {
+ return make_error_code(ParseError::Unsuitable);
+ }
+ DefaultLanguage = Language;
StyleSet.Add(std::move(DefaultStyle));
}
*Style = *StyleSet.Get(Language);
diff --git a/clang/unittests/Format/ConfigParseTest.cpp b/clang/unittests/Format/ConfigParseTest.cpp
index cc42642f99252..4c17c1a50face 100644
--- a/clang/unittests/Format/ConfigParseTest.cpp
+++ b/clang/unittests/Format/ConfigParseTest.cpp
@@ -1230,6 +1230,12 @@ TEST(ConfigParseTest, ParsesConfigurationWithLanguages) {
IndentWidth, 56u);
}
+TEST(ConfigParseTest, AllowCppForC) {
+ FormatStyle Style = {};
+ Style.Language = FormatStyle::LK_C;
+ EXPECT_EQ(parseConfiguration("Language: Cpp", &Style), ParseError::Success);
+}
+
TEST(ConfigParseTest, UsesLanguageForBasedOnStyle) {
FormatStyle Style = {};
Style.Language = FormatStyle::LK_JavaScript;
|
HazardyKnusperkeks
approved these changes
Mar 26, 2025
HazardyKnusperkeks
approved these changes
Mar 27, 2025
Contributor
Author
|
/cherry-pick 05fb840 |
Member
|
/pull-request #133216 |
swift-ci
pushed a commit
to swiftlang/llvm-project
that referenced
this pull request
Mar 29, 2025
Fix llvm#132832 (cherry picked from commit 05fb840)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fix #132832